﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Json;
using WLFramework.Web;

namespace SampleSite.admin.ajax
{
    /// <summary>
    /// getPositionsByOrg 的摘要说明
    /// </summary>
    public class getPositionsByOrg : AjaxHttpHandler
    {
        public override void OnProcessRequest(HttpContext context)
        {
            int? oid = context.GetValueByName<int?>("O_ID");
            int? uid = context.GetValueByName<int?>("U_ID");
            if (oid == null)
                return;
            if (uid == null)
                return;

            using (var db = new PermissionContext())
            {
                var positions = from p in db.View_Positions.Where(k => k.P_Org == oid && k.P_State==1)
                                join i in db.View_UserPosition.Where(k => k.P_State == 1 && k.U_ID == uid) on p.P_ID equals i.P_ID
                                    into temp
                                from t in temp.DefaultIfEmpty()
                                select new
                                {
                                    p,
                                    t
                                };


                context.Response.Write(JSON.Instance.ToJSON(new
                {
                    total = positions.Count(),
                    rows = positions.Select(k => new
                    {
                        IsChecked = k.t.P_State == null ? false : true,
                        P_ID = k.p.P_ID,
                        P_Org = k.p.P_Org,
                        P_Level = k.p.P_Level,
                        P_CName = k.p.P_CName,
                        P_EName = k.p.P_EName,
                        O_CName = k.p.O_CName
                    }).ToList()
                }));
            }
        }
    }
}